﻿namespace EFS.RC.CC.Messages.Agents
{
	/// Class Status
	public class Status : EFS.Common.Serialization.ISerializable
	{
		/// Cosntructor
		public Status()
		{
			// set default
			Clear();
		}

		/// Clear
		private void Clear()
		{
			SIP = null;
			FirstName = null;
			LastName = null;
			IsOnline = false;
			InSession = false;
			InSessionWrap = false;
			IsAvailable = false;
			CustomerInfo = null;
		}

		/// Property SIP: Get, Set
		public System.String SIP;

		/// Property FirstName: Get, Set
		public System.String FirstName;

		/// Property LastName: Get, Set
		public System.String LastName;

		/// On-line status
		public System.Boolean IsOnline;

		/// Session status
		public System.Boolean InSession;

		/// Session Wrap status
		public System.Boolean InSessionWrap;

		/// Available for conversation
		public System.Boolean IsAvailable;

		/// If set, means agent is talking to a customer
		public System.String CustomerInfo;

		/// Property ConversationInfo: Get, Set
		public EFS.RC.CC.Messages.Communicator.Lync.Conversation.Info ConversationInfo;

		/// Property ACD: Get, Set
		public System.String[] ACD;

		/// Property Skills: Get, Set
		public EFS.RC.CC.Messages.Agents.Skills.Skill[] Skills;

		/// Test if has the specified skill
		public System.Boolean HasSkill(System.Int32 skillID)
		{
			// verify array
			if (null != Skills)
			{
				// cicle all
				foreach (EFS.RC.CC.Messages.Agents.Skills.Skill skill in Skills)
				{
					// test
					if (skill.Id == skillID)
					{
						return true;
					}
				}
			}

			// no
			return false;
		}

		/// GetSkill
		public EFS.RC.CC.Messages.Agents.Skills.Skill GetSkill(System.Int32 skillID)
		{
			// cicle all
			foreach (EFS.RC.CC.Messages.Agents.Skills.Skill skill in Skills)
			{
				if (skillID == skill.Id)
				{
					return skill;
				}
			}

			// none found
			return null;
		}

		/// Check agent in ACD
		public System.Boolean CheckACD(
			System.String sip,
			System.Boolean? isOnline,
			System.Boolean? inSession,
			System.Boolean? inWrap,
			System.Boolean? isAvailable,
			System.Int32? skillID,
			System.Boolean acdInclude,
			System.String acdMatch)
		{
			// test sip
			if (EFS.Common.Verify.String(sip) && 0 != System.String.Compare(sip, SIP, System.StringComparison.CurrentCultureIgnoreCase))
			{
				return false;
			}

			// isOnline
			if (isOnline.HasValue && isOnline.Value != IsOnline)
			{
				return false;
			}

			// inSession
			if (inSession.HasValue && inSession.Value != InSession)
			{
				return false;
			}

			// inWrap
			if (inWrap.HasValue && inWrap.Value != InSessionWrap)
			{
				return false;
			}

			// isAvailable
			if (isAvailable.HasValue && isAvailable.Value != IsAvailable)
			{
				return false;
			}

			// skillID
			if (skillID.HasValue)
			{
				if (!HasSkill(skillID.Value))
				{
					return false;
				}
			}

			// only if a valid array
			if (EFS.Common.Verify.Array(ACD))
			{
				// cicle all
				foreach (System.String acd in ACD)
				{
					// check ACD
					if (0 == System.String.Compare(acd, acdMatch, System.StringComparison.CurrentCultureIgnoreCase))
					{
						return acdInclude;
					}
				}
			}

			// match
			return !acdInclude;
		}

		/// Serialize
		public void Serialize(EFS.Common.Serialization.XmlWriter xmlWriter)
		{
			// write
			xmlWriter.Write("SIP", SIP);
			xmlWriter.Write("FirstName", FirstName);
			xmlWriter.Write("LastName", LastName);
			xmlWriter.Write("IsOnline", IsOnline);
			xmlWriter.Write("InSession", InSession);
			xmlWriter.Write("InSessionWrap", InSessionWrap);
			xmlWriter.Write("IsAvailable", IsAvailable);
			xmlWriter.Write("CustomerInfo", CustomerInfo);
			xmlWriter.WritePolymorphic("ConversationInfo", ConversationInfo);
			xmlWriter.Write("ACDs", "ACD", ACD);
			xmlWriter.Write<EFS.RC.CC.Messages.Agents.Skills.Skill>("Skills", "Skill", Skills); 
		}

		/// Deserialize
		public void Deserialize(EFS.Common.Serialization.XmlReader xmlReader)
		{
			// read
			xmlReader.Read("SIP", ref SIP);
			xmlReader.Read("FirstName", ref FirstName);
			xmlReader.Read("LastName", ref LastName);
			xmlReader.Read("IsOnline", ref IsOnline);
			xmlReader.Read("InSession", ref InSession);
			xmlReader.Read("InSessionWrap", ref InSessionWrap);
			xmlReader.Read("IsAvailable", ref IsAvailable);
			xmlReader.Read("CustomerInfo", ref CustomerInfo);
			ConversationInfo = (EFS.RC.CC.Messages.Communicator.Lync.Conversation.Info)xmlReader.ReadPolymorphic("ConversationInfo");
			xmlReader.Read("ACDs", "ACD", ref ACD);
			xmlReader.Read<EFS.RC.CC.Messages.Agents.Skills.Skill>("Skills", "Skill", ref Skills); 
		}
	}
}